﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FileUpload.DAL;
using FileUpload.DAL.Entities;
using System.Text;

namespace FileUpload.Controllers
{
    public class ProductsController : Controller
    {
        private FileUploadContext db = new FileUploadContext();

        // GET: Products
        public async Task<ActionResult> Index()
        {
            return View(await db.Products.ToListAsync());
        }

        // GET: Products/Details/5
        public async Task<ActionResult> Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = await db.Products.FindAsync(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        // GET: Products/Create
        public ActionResult Create()
        {
            ViewBag.token = Guid.NewGuid().ToString();
            return View();
        }

        // POST: Products/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Id,Name,Photo1")] Product product, HttpPostedFileBase photo1, HttpPostedFileBase photo2, HttpPostedFileBase photo3)
        {
            if (ModelState.IsValid)
            {
                product.Id = Guid.NewGuid();
                if (photo1 != null||product.Photo1!=null)
                {
                    byte[] imageByte = new byte[photo1.ContentLength];
                    photo1.InputStream.Read(imageByte, 0, photo1.ContentLength);
                    product.Photo1 = imageByte;
                }
                if (photo2 != null)
                {
                    byte[] imageByte = new byte[photo2.ContentLength];
                    photo1.InputStream.Read(imageByte, 0, photo2.ContentLength);
                    product.Photo2 = imageByte;
                }
                if (photo3 != null)
                {
                    byte[] imageByte = new byte[photo3.ContentLength];
                    photo3.InputStream.Read(imageByte, 0, photo3.ContentLength);
                    product.Photo3 = imageByte;
                }
                db.Products.Add(product);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(product);
        }

        // GET: Products/Edit/5
        public async Task<ActionResult> Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = await db.Products.FindAsync(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        // POST: Products/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "Id,Name,Photo1,Photo2,Photo3")] Product product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(product);
        }

        // GET: Products/Delete/5
        public async Task<ActionResult> Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = await db.Products.FindAsync(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        // POST: Products/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(Guid id)
        {
            Product product = await db.Products.FindAsync(id);
            db.Products.Remove(product);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        public ActionResult Moblie()
        {
            return View();
        }

    }
}
